package com.hmall.search.web;

import com.hmall.common.request.PortalPageDTO;
import com.hmall.common.response.PageResultVO;
import com.hmall.search.service.SearchService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description:es搜索
 * @Date: 2023/4/9 17:26
 */
@RestController
@RequestMapping("/search")
public class SearchController {


    @Resource
    private SearchService searchService;

    /**
     * 条件分页查询
     *
     * @param pageDTO
     * @return
     * @throws IOException
     */
    @PostMapping("/list")
    public PageResultVO searchPage(@RequestBody PortalPageDTO pageDTO) throws IOException {
       /* SearchRequest request = new SearchRequest("hmall");
        //上一次查询的最后一条数据
        request.source().query(QueryBuilders.matchAllQuery());
        request.source().sort("id", SortOrder.ASC);
        request.source().size(pageDTO.getSize());
        String[] searchAfter = new String[]{"100002465022"};
        request.source().searchAfter(searchAfter);
        SearchResponse res = client.search(request, RequestOptions.DEFAULT);
        System.out.println(res);*/

        return searchService.getSearchResult(pageDTO);

    }

    /**
     * 聚合索引库
     */
    @PostMapping("/filters")
    public Map<String, List<String>> getFilters(@RequestBody PortalPageDTO pageDTO) {

        return searchService.getFilters(pageDTO);
    }

    /**
     * 搜索自动补全
     */
    @GetMapping("/suggestion")
    public List<String> getSuggestions(String key) {
        return searchService.getSuggestions(key);
    }
}